<?php

/**
 * Description of Abstract
 *
 * 
 */
abstract class Collection {
    //put your code here
    protected $collection = array();
    protected $currentIndex = 0;
    
    
    public function setCollection($collection) {
        if (is_array($collection))
            $this->collection = $collection;
    }
    
    public function limit($limit,$offset=0){
        $collection = array();
        $i=0;
        foreach ($this->collection AS $key=>$value){
            if ($i>=$offset && $i<$offset+$limit)
                $collection[$key] = $value;
            $i++;
        }
        $this->collection = $collection;        
    }
    
    public function clearCollection(){
        $this->collection = array();
    }

    public function put($obj,$id = null){        
        if ($id == null)
            $this->collection[] = $obj;
        else{            
            $this->collection[$id] = $obj;
        }
    }
    public function issetKey($id){
        if (isset($this->collection[$id]))
                return true;
        else return false;
    }
    public function pop(){
        array_pop($this->collection);
    }
    
    public function shift(){
        array_shift($this->collection);
    }
    
    public function removeById($id){        
        if (isset($this->collection[$id])){
            unset($this->collection[$id]);            
        }
    }
    
    public function count(){
        return count($this->collection);
    }
    
    public function get($id){
        if (isset($this->collection[$id]))
            return $this->collection[$id];
        else
            return null;
    }
    
    public function getAllInArray(){
        return $this->collection;
    }
    
    public function next(){
	$this->currentIndex++;
        return next($this->collection);
    }
    public function iterator(){
	if($this->currentIndex == 0)
	{
	    $this->currentIndex++;
	    return $this->current();
	}
	else
	    return $this->next();
    }    
    public function prev(){
	$this->currentIndex--;
        return prev($this->collection);
    }
    
    public function isLast(){

	if($this->currentIndex == $this->count())
	    return true;
	else
	    return false;
    }    
    
    public function current(){
        return current($this->collection);
    }
    public function reset(){
        $this->currentIndex = 0;
        return reset($this->collection);
    }
    public function getCurrentIndex()
    {
	return $this->currentIndex;
    }
    
    public function getKey()
    {
	return key($this->collection);
    }
    public function end(){
        return end ($this->collection);
    }
    
    public function __toSelectArray($columnName,$elementZero=null,$withId=true){
        $out = array();
        $this->reset();
        if ($elementZero!=null) $out[0] = $elementZero;
        while ($item = $this->iterator()){
            $method = 'get'.$columnName;
            $out[$item->getId()] = $item->$method();
            if ($withId) $out[$item->getId()].= ' ('.$item->getId().')';
        }        
        $this->reset();
        return $out;
    }
}
?>
